package com.flot.yazb.base.client;

import android.util.Log;

import com.flot.yazb.BuildConfig;
import com.flot.yazb.base.BApplication;
import com.flot.yazb.base.host.HostAnoy;
import com.flot.yazb.aop.stub.HttpLoggingInterceptor;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;


public class RetrofitFactory extends Converter.Factory {
    private static RetrofitFactory mInstance;
    private Retrofit retrofit;

    public static RetrofitFactory getInstance() {
        if (mInstance == null) {
            mInstance = new RetrofitFactory();

        }
        return mInstance;
    }

    public RetrofitFactory() {
        RxJavaCallAdapterFactory adapyter = RxJavaCallAdapterFactory.create();
        retrofit = new Retrofit.Builder()
                .baseUrl(BApplication.APIHOST)
                .addCallAdapterFactory(adapyter)
                .addConverterFactory(CustomGsonConverterFactory.create())
                .client(okHttp())
                .build();
    }


    public <T> T create(Class<T> mClass) {
        HostAnoy.NameSpace ns = mClass.getAnnotation(HostAnoy.NameSpace.class);
        if (null == ns) {
            try {
                retrofit = retrofit.newBuilder().baseUrl(BApplication.APIHOST).build();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            retrofit = retrofit.newBuilder().baseUrl(ns.value()).build();
        }
        return retrofit.create(mClass);
    }

    private OkHttpClient okHttp() {
        OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(Interceptor.Chain chain, String message) {
                Log.d(getTag(chain.request().url()), message);
            }

            private String getTag(HttpUrl url) {
                if (url == null)
                    return "okhttp";
                return "okhttp-" + url.uri().getPath();
            }
        });
        interceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.HEADERS);
        okBuilder.addInterceptor(interceptor);
        //timeout设置
        okBuilder.connectTimeout(30000L, TimeUnit.MILLISECONDS)
                .readTimeout(30000L, TimeUnit.MILLISECONDS);
        /*
        cookieJar = new PersistentCookieJar(new SetCookieCache(),
              new SharedPrefsCookiePersistor(CFApplication.getApplication()));
        okBuilder.cookieJar(cookieJar);
        */

        okBuilder.addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                final Request origRequest = chain.request();
                Request request = origRequest.newBuilder()
                        .build();
                return chain.proceed(request);
            }
        });

        return okBuilder.build();
    }

}
